/* 
Given two arrays A[6] and B[3], both the arrays have 3 elements each. Both of them are sorted, need to return the merged array A[] after merging with array B[]. Need to do this inplace.

*/

#include<iostream.h>
#include<conio.h>
void merge(int arrA[], int arrB[])
{
     int j=2;
     int k=2;
     for(int i=5; i>=0; i--)
      {
             if((j == -1) || (k == -1))
             {
                   if(j==-1)
                   {   
                      arrA[i] = arrB[k];
                      k--;
                   }
                   else
                   {
                       arrA[i] = arrA[j];
                       j--;
                   }
             }
             else   if(arrA[j] > arrB[k])
             {
                  arrA[i] = arrA[j];
                  j--;
             }
             else
             {
                 arrA[i] = arrB[k];
                 k--;
             }
      }
      for(int i=0; i<6;i++)
        cout<<arrA[i]<<"   ";
}
int main()
{
    int   arrA[6] = {0,8,34}, arrB[3] = {4,9,99};
    merge(arrA, arrB);
    getch();
    return 0;
}

